
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实现移动的矩形</title>
    <style>
        canvas{
            border: 1px solid #333;
        }
    </style>
</head>
<body>
    <canvas id="canvas" width="600" height="600"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');

        // 矩形沿着坐标系对角线移动
        let x = 0;
        let y = 0;

        const interval = setInterval(() => {
            ctx.clearRect(x,y,100,100);
            x++;
            y++;
            ctx.fillStyle = "red";
            ctx.fillRect(x,y,100,100);

            if(x === 500){
                clearInterval(interval);
            }
        },20);

    </script>
</body>
</html>